package com.yanqu.road.dao.impl.activity.flowerstudy;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.flowerstudy.data.FlowerStudyUserWarReport;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FlowerStudyExamReportDataDaoImpl extends TempDao {
    public static String getFlowerStudyUserWarReportSql = "select * from `t_u_flower_study_activity_user_war_report` where `activityId`=? and `userId`=?";
    public List<FlowerStudyUserWarReport> getFlowerStudyUserWarReportList(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<FlowerStudyUserWarReport> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getFlowerStudyUserWarReportSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parseData(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertFlowerStudyUserWarReportListSql = "insert into `t_u_flower_study_activity_user_war_report`(activityId,userId,roomId,roomType,score,answerTime,rank,reward,settlementTime) values (?,?,?,?,?,?,?,?,?)";
    public boolean addDataList(List<FlowerStudyUserWarReport> flowerStudyUserWarReportList) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getUserDb());

        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (FlowerStudyUserWarReport flowerStudyUserWarReport:flowerStudyUserWarReportList){
            int i = 1;
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();

            para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserWarReport.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserWarReport.getUserId()));
            para.put(i++, new DbParameter(Types.VARCHAR, flowerStudyUserWarReport.getRoomId()));
            para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserWarReport.getRoomType()));
            para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserWarReport.getScore()));
            para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserWarReport.getAnswerTime()));
            para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserWarReport.getRank()));
            para.put(i++, new DbParameter(Types.VARCHAR, flowerStudyUserWarReport.getReward()));
            para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserWarReport.getSettlementTime()));

            mapList.add(para);
        }

        if (getDb().executeBatchQuery(insertFlowerStudyUserWarReportListSql, mapList) > -1) {
            return true;
        }
        return false;

    }

    public static String insertFlowerStudyUserWarReportSql = "insert into `t_u_flower_study_activity_user_war_report`(activityId,userId,roomId,roomType,score,answerTime,rank,reward,settlementTime) values (?,?,?,?,?,?,?,?,?)";
    public boolean addData(FlowerStudyUserWarReport flowerStudyUserWarReport) {
        flowerStudyUserWarReport.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserWarReport.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserWarReport.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, flowerStudyUserWarReport.getRoomId()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserWarReport.getRoomType()));
        para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserWarReport.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserWarReport.getAnswerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserWarReport.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, flowerStudyUserWarReport.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserWarReport.getSettlementTime()));
        if (getDb().executeNoneQuery(insertFlowerStudyUserWarReportSql, para) > 0) {
            return true;
        }
        return false;

    }

    protected FlowerStudyUserWarReport parseData(ResultSet rs) throws SQLException {
        FlowerStudyUserWarReport flowerStudyUserWarReport = new FlowerStudyUserWarReport();
        flowerStudyUserWarReport.setActivityId(rs.getInt("activityId"));
        flowerStudyUserWarReport.setUserId(rs.getLong("userId"));
        flowerStudyUserWarReport.setRoomId(rs.getString("roomId"));
        flowerStudyUserWarReport.setRoomType(rs.getInt("roomType"));
        flowerStudyUserWarReport.setScore(rs.getLong("score"));
        flowerStudyUserWarReport.setAnswerTime(rs.getLong("answerTime"));
        flowerStudyUserWarReport.setRank(rs.getInt("rank"));
        flowerStudyUserWarReport.setReward(rs.getString("reward"));
        flowerStudyUserWarReport.setSettlementTime(rs.getLong("settlementTime"));

        return flowerStudyUserWarReport;
    }
}
